package com.ymtc.ibm.code;


import com.ibm.mq.*;
import com.ibm.mq.constants.MQConstants;

/**
 */
public class MQClientPutPriority {
	private String hostname = "127.0.0.1";
	private int port = 1414; 
	private String channel = "CLIENT.CHL"; 
	private String qManager = "QMA"; 
	private MQQueueManager qMgr; 
	private static String qName = "Q.PRIORITY";
	private MQQueue queue;

	public static void main(String[] args) {		
		MQClientPutPriority sp = new MQClientPutPriority();
		try {
			sp.MQStart();
			sp.sendMsgToQueue();
			sp.MQStop();
		} catch (Exception e) {
			e.printStackTrace(System.out);
			sp.MQStop();
			System.exit(2);
		}
	}


	public MQClientPutPriority() {
	}
	
	public void MQStart() {
		try {
			MQEnvironment.hostname = hostname; 
			MQEnvironment.channel = channel; 
			MQEnvironment.port = port;
			MQEnvironment.CCSID = 1208;
			
			//Create a connection to the queue manager
			qMgr = new MQQueueManager(qManager);
			int openOptions = MQConstants.MQOO_OUTPUT | MQConstants.MQOO_FAIL_IF_QUIESCING | MQConstants.MQOO_BIND_NOT_FIXED ;
			queue = qMgr.accessQueue(qName, openOptions);
			System.out.println(">>> Open Queue successful!");
		} catch (MQException ex) {
			System.out.println("WebSphere MQ error occurred: Competion code"+ ex.completionCode	+ "Reason Code"	+ ex.reasonCode);
		}
	}

	public void MQStop() {
		try {
			queue.close();
			qMgr.disconnect();
		} catch (MQException ex) {
		}
	}

	public void sendMsgToQueue() {
		try {
			MQPutMessageOptions pmo = new MQPutMessageOptions();
			//pmo.options = pmo.options + MQConstants.MQPMO_SYNCPOINT;
			pmo.options = pmo.options + MQConstants.MQPMO_LOGICAL_ORDER;
			pmo.options = pmo.options + MQConstants.MQPMO_NEW_MSG_ID;
			pmo.options = pmo.options + MQConstants.MQPMO_NEW_CORREL_ID;
			//pmo.options = pmo.options + MQConstants.MQRO_COPY_MSG_ID_TO_CORREL_ID;
			MQMessage msg = new MQMessage();
			System.out.println(msg.priority);
			for (int i=0;i<10;i++) {
				msg.priority = i;
				msg.writeString(">>>>>> Priority is ------" + i);										
				queue.put(msg, pmo);
				System.out.println(">>> Put Msg Priority is --- " + i);
				msg.clearMessage();	
				Thread.sleep(1000);
			}			
			System.out.println(">>> Put OK!!!");
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
	}
}